﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using YY.UI.BaseDataSvc;
using YY.Common;

namespace YY.UI
{
    public partial class CheckOrderFrm : BaseFrm
    {
        /// <summary>
        /// 核销情况
        /// </summary>
        List<QCheckOrder> checkOffs = null;

        public CheckOrderFrm()
        {
            InitializeComponent();
            this.BaseDataProxy = new YY.UI.BaseDataSvc.BaseDataClient();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {

            DateTime dtStart = this.dtpSDate.Value.Date;
            DateTime dtEnd = this.dtpEDate.Value.AddDays(1).Date.AddSeconds(-1);
            checkOffs = this.BaseDataProxy.GetCheckOffsByApplyDate(dtStart, dtEnd).ToList();

            if (checkOffs != null)
            {
                this.dataGridView1.DataSource = checkOffs.ToList();
            }
        }

        /// <summary>
        /// 初始化报关信息表格列
        /// </summary>
        private void initBGDgvColumns()
        {
            //初始化报关核销数据列
            this.dataGridView1.AutoGenerateColumns = false;
            this.dataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.EnableResizing;//AutoSize十分影响性能
            Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Id", "核销Id", false, true, null);
            Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Title", "核销单抬头", true, false, null);
            Common.AddDgvColumn(this.dataGridView1, new DataGridViewCalendarColumn(), "BackTime", "回单时间", true, false, null);
            Common.AddDgvColumn(this.dataGridView1, new DataGridViewCalendarColumn(), "BackClientTime", "退回客户时间", true, false, null);
            Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "ExpressNo", "快递单号", true, false, null);
            Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "ExtractOrderNo", "提单号", true, false, null);
        }


        private void CheckOrderFrm_Load(object sender, EventArgs e)
        {
            this.dtpEDate.Value = DateTime.Now;
            this.dtpSDate.Value = DateTime.Now.AddDays(-7);
            this.BaseDataProxy.Open();
            initBGDgvColumns();
        }

        #region 核销汇总
        /// <summary>
        /// 核销汇总
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnHXSummary_Click(object sender, EventArgs e)
        {
            if (this.checkOffs == null)
            {
                return;
            }


            ExcelCommon excel = new ExcelCommon();
            try
            {
                excel.Create();
                excel.ws = excel.AddSheet("核销汇总");
                excel.app.Visible = true;
                excel.SetCellValue(excel.ws, 1, 1, "退回日期");
                excel.SetCellValue(excel.ws, 1, 2, "提单号");
                excel.SetCellValue(excel.ws, 1, 3, "核销单抬头");
                excel.SetCellValue(excel.ws, 1, 4, "客户");
                excel.SetCellValue(excel.ws, 1, 5, "寄出日期");
                excel.SetCellValue(excel.ws, 1, 6, "快递单号");
                excel.SetCellValue(excel.ws, 1, 7, "起运港口");
                excel.SetCellValue(excel.ws, 1, 8, "申报日期");

                int flag = 0;
                for (int i = 0; i < this.checkOffs.Count(); i++)
                {
                    //只需要导出有输入的核销单抬头。如果没有输入的话就不用显示出来的
                    if (!string.IsNullOrEmpty(this.checkOffs[i].Title))
                    {
                        excel.SetCellValue(excel.ws, 2 + flag, 1, this.checkOffs[i].BackTime);
                        excel.SetCellValue(excel.ws, 2 + flag, 2, this.checkOffs[i].ExtractOrderNo);
                        excel.SetCellValue(excel.ws, 2 + flag, 3, this.checkOffs[i].Title);
                        excel.SetCellValue(excel.ws, 2 + flag, 4, this.checkOffs[i].ClientFullName);
                        excel.SetCellValue(excel.ws, 2 + flag, 5, this.checkOffs[i].BackClientTime);
                        excel.SetCellValue(excel.ws, 2 + flag, 6, this.checkOffs[i].ExpressNo);
                        excel.SetCellValue(excel.ws, 2 + flag, 7, this.checkOffs[i].StartPort);
                        excel.SetCellValue(excel.ws, 2 + flag, 8, this.checkOffs[i].ApplyDate);
                        flag++;
                    }
                }
                excel.SetCellProperty(excel.ws, 1, 1, 100, 8, 18, true, "宋体", Microsoft.Office.Interop.Excel.Constants.xlColor3, Microsoft.Office.Interop.Excel.Constants.xlLeft, false);
                excel.SetCellProperty(excel.ws, 2, 1, 100, 8, 15, false, "宋体", Microsoft.Office.Interop.Excel.Constants.xlColor3, Microsoft.Office.Interop.Excel.Constants.xlLeft, false);
                excel.ColumnsAutoFit(excel.ws, 1, 1, 100, 8);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                excel.Dispose();
            }
        }
        #endregion

        /// <summary>
        /// DataGrid绘制颜色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            if (e.RowIndex < this.dataGridView1.Rows.Count)
            {
                try
                {
                    if (this.dataGridView1.Rows[e.RowIndex].Cells[4].Value != null && !string.IsNullOrEmpty(this.dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString()))
                    {
                        this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Green;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
    }
}
